package model;

import java.util.List;
import java.util.Map;
import java.util.*;
import util.DBUtil;
public class UserService {

    private DBUtil db = null;
    public UserService() {
        db=new DBUtil();
    }
    public int updateUser(String pw,String tel,String addr,String id){
        String sql = "update user set password=?,telephone=?,address=? where id=?";
        String[] params = {pw,tel,addr,id};
        return db.update(sql,params);
    }
    public int updateUser(Map<String,String> m){
        String sql = "update user set username=? ,password=?,telephone=?,address=? where id=?";
        String[] params = {m.get("username"),m.get("password"),m.get("telephone"),m.get("address"),m.get("id")};
        //输出params数组
        for(String s:params)
            System.out.println(s);


        return db.update(sql,params);
    }

    /**
     * 根据id查找用户
     * @param id
     * @return 封装了一个用户信息的map对象
     */
    public Map<String,String> getUserById(String id){
        String sql="select * from user where id= ? ;";
        String[] params = {id};
        return db.getMap(sql,params);

    }
    //根据用户名模糊查找所以用户;
    public List<Map<String, String>> getUsers(String username){
        String sql = "select * from user";
        String[] params = null;
        if(username!=null&&!"".equals(username)) {
            sql=sql+" where username like ?";
            params = new String[] {
                    "%"+username+"%"
            };

        }
        return db.getList(sql, params);

    }
    public int delUser(int id){
        String sql="delete from user where id="+id;
        return db.update(sql);
    }

    public static void main(String[] args) {
        UserService userService = new UserService();
        List<Map<String, String>> list=userService.getUsers(null);
        for(Map<String, String> u:list)
            System.out.print(u.get("username"));

    }


}
